package common.utils;

import cn.hutool.core.lang.caller.CallerUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.LogFactory;
import common.variables.StringIdentifiers;
import lombok.var;

public class LogUtil {


    static int count;

    public static void log(Object value, Object... args) {
        var msg = StringUtil.format(value, args);
        var stackTraceElements = new Throwable().getStackTrace();
        var targetStackTraceElement = LinqUtil.filterOne(stackTraceElements,
                m -> !LogUtil.class.getName().equals(m.getClassName()));
        System.out.println(StringUtil.format(
                "\r\n[{}][{}][log]\r\n[{}]\r\n" +
                        "╭───────────────────────────────────────────────────────────────────────────────────────────╮" +
                        "\r\n{}\r\n" +
                        "╰━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╯\r\n",
                ++count, TimeUtil.getNowString(TimeUtil.format_ymdhmss),
                targetStackTraceElement, msg));
    }

    public static void info(Object value, Object... args) {
        log(value, args);
    }

    public static void debug(Object value, Object... args) {
        log(value, args);
    }

    public static void json(Object value) {
        if (value == null)
            log("null");
        log(JSONUtil.toJsonPrettyStr(value));
    }

    public static void log() {
        log(null);
    }

    public static void logJson(Object value) {
        json(value);
    }

    private static String format(Object value) {
        return StringIdentifiers.wrap + value + StringIdentifiers.logEnd;
    }

    public static void writeLine() {
        var log = LogFactory.get(CallerUtil.getCallerCaller());
        log.info("");
    }

    public static void debug(Object value) {
        var log = LogFactory.get(CallerUtil.getCallerCaller());
        log.info(value.toString());
    }

    public static void warn(Object value, Object... arguments) {
        var log = LogFactory.get(CallerUtil.getCallerCaller());
        log.warn(format(value), arguments);
    }

    public static void warn(Exception ex) {
        var log = LogFactory.get(CallerUtil.getCallerCaller());
        log.warn(ex.getMessage());
    }

    public static void error(Exception ex) {
        var log = LogFactory.get(CallerUtil.getCallerCaller());
        log.error(ex);
    }

    public static void error(Object value) {
        var log = LogFactory.get(CallerUtil.getCallerCaller());
        log.error(format(value));
    }

    public static void record(Object value) {
        try {
            info("try to record: " + value);
            var now = TimeUtil.getNow();
            var path = String.format("app_data/error/%s.log", TimeUtil.getString(now, "yyyy-MM-dd"));
            var text = String.format("[%s]\r\n%s\r\n", TimeUtil.getString(now, "yyyy-MM-dd HH:mm:ss.SSS"), value);
            FileUtil.appendLine(path, text);
        } catch (Exception ex) {
            error("error occurred when recording log: " + ex.getMessage());
        }
    }

    //todo log添加颜色 信息-绿色 警告-黄色 错误-红色 有时候生效，有时候无效
    public static void test() {
//        MongoDbUtil.set("test", "hello");
//        LogUtil.info(MongoDbUtil.get("test"));
    }

}
